package com.szholly.pro.watergas_server.common.web;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;

public class RequestUnit {

	static List<String> parsePatterns = Arrays.asList("yyyy-MM-dd HH:mm:ss",
			"yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd HH:mm",
			"yyyy-MM-dd", "yyyy/MM/dd");

	public static String getString(String paramName) {
		String value = getParameter(paramName);
		return value;
	}
	
	/**
	 * 获取逗号分隔的数组
	 * @param paramName
	 * @return
	 */
	public static String[] getStringArray(String paramName) {
		String value = getParameter(paramName);
		if(value == null){
			return null;
		}
		String[] result = value.split(",");
		return result;
	}
	
	/**
	 * 获取标准的数组	
	 * @param paramName
	 * @return
	 */
	public static String[] getArray(String paramName){
		String[] values = WebContext.getRequest().getParameterValues(paramName);
		return values;
	}

	public static Integer getInt(String paramName) {
		try {
			int value = Integer.parseInt(getParameter(paramName).trim());
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public static BigDecimal getBigDecimal(String paramName) {
		try {
			double dValue = getDouble(paramName);
			BigDecimal value = new BigDecimal(dValue);
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public static Double getDouble(String paramName) {
		try {
			double value = Double.parseDouble(getParameter(paramName));
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public static Date getDate(String paramName) {
		try {
			Date value = DateUtil.parseDate(getParameter(paramName),
					parsePatterns);
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public static Timestamp getTimestamp(String paramName) {
		try {
			String valueStr = getParameter(paramName);
			Timestamp value = parseDate(valueStr);
			return value;
		} catch (Exception e) {
			return null;
		}
	}
	
	public static HashMap<String, Object> getHashMapParmO() {
		Enumeration<String> names = WebContext.getRequest().getParameterNames();
		try {
			WebContext.getRequest().setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashMap<String, Object> parm = new HashMap<String, Object>();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = getString(name);
			if (name.indexOf("@likeLeft") > 0) {
				if (value != null && !value.equals("")) {
					value = "%" + value;
				}
				name = name.replaceAll("@likeLeft", "");
			} else if (name.indexOf("@likeRight") > 0) {
				if (value != null && !value.equals("")) {
					value = value + "%";
				}
				name = name.replaceAll("@likeRight", "");
			} else if (name.indexOf("@like") > 0) {
				if (value != null && !value.equals("")) {
					value = "%" + value + "%";
				}
				name = name.replaceAll("@like", "");
			}
			parm.put(name, value);
		}
		return parm;
	}

	public static HashMap<String, String> getHashMapParmS() {
		Enumeration<String> names = WebContext.getRequest().getParameterNames();
		HashMap<String, String> parm = new HashMap<String, String>();
		String waterType=RequestUnit.getString("WATER_TYPE");
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = getString(name);
			if (name.indexOf("@likeLeft") > 0) {
				if (value != null && !value.equals("")) {
					value = "%" + value;
				}
				name = name.replaceAll("@likeLeft", "");
			} else if (name.indexOf("@likeRight") > 0) {
				if (value != null && !value.equals("")) {
					value = value + "%";
				}
				name = name.replaceAll("@likeRight", "");
			} else if (name.indexOf("@like") > 0) {
				if (value != null && !value.equals("")) {
					value = "%" + value + "%";
				}
				name = name.replaceAll("@like", "");
			}
			parm.put(name, value);
		}
		parm.put("WATER_TYPE", waterType);
		return parm;
	}

	public static String getParameter(String paramName) {
		String result = WebContext.getRequest().getParameter(paramName);
		if (result != null) {
			return result;
		}

		return result;
	}

	public static void setAttr(String key, Object value) {
		WebContext.getRequest().setAttribute(key, value);
	}

	public static Timestamp parseDate(String dateStr) throws DateParseException {
		for (String formatStr : parsePatterns) {

			SimpleDateFormat df = new SimpleDateFormat(formatStr);
			try {
				Date date = df.parse(dateStr);
				Timestamp value = new Timestamp(date.getTime());
				return value;
			} catch (Exception e) {
			}
		}
		return null;
	}
}